<?php

if (!defined("BASEPATH"))
    exit("No direct script access allowed");
require_once APPPATH . "controllers/dienhoa-administrator/base_controller" . EXT;

class Account extends Base_controller {

    public function __construct() {
        parent::__construct();
        $this->check_login();
    }

    public function view_profile() {

        $data = array();
        $data = $this->SetMenu("");
        $data["id"] = $this->session->userdata('ID');
        $data["checkAdmin"] = $this->session->userdata("user_name");
        $data["infor"] = $this->admin_model->getInfoAdmin($data["id"])->row();
        $data["mod"] = $this->admin_model->getInforMod()->result();
        $group_list = $this->admin_model->getGroup();
        if ($group_list->num_rows() > 0) {
            foreach ($group_list->result() as $rows) {
                $data["nhom"][] = array(
                    "groupName" => $rows->admin_group_name,
                    "groupID" => $rows->admin_group_id,
                );
            }
        } else {
            $data["nhom"] = "";
        }

        $this->render($this->load->view("admin/account/account_view", $data, TRUE));
    }

    //form tao tai khoan
    public function form_creat_acc() {
        $this->permissions_view();
        $data = array();
        $data = $this->SetMenu("");
        $data["group_admin"] = $this->admin_model->getGroup()->result();
        $this->render($this->load->view("admin/account/creat_account_view", $data, TRUE));
    }

    //them tai khoan
    public function creat_acc() {
        $this->permission_del_edit();
        $data = array();

        $this->form_validation->set_rules("txtName", "tên người dùng", "trim|required|xss_clean");
        $this->form_validation->set_rules("txtUser", "tên đăng nhập", "trim|required|xss_clean|callback_check_user");
        $this->form_validation->set_rules("txtPass", "Mật khẩu", "trim|required|xss_clean|matches[txtRePass]");
        $this->form_validation->set_rules("txtRePass", "Nhập lại mật khẩu", "trim|required|xss_clean");
        $this->form_validation->set_rules("txtEmail", "Email", "trim|required|valid_email|callback_check_email");
        $this->form_validation->set_rules("optGroup", "Nhóm tài khoản", "required");

        $data["name"] = $this->input->post("txtName");
        $data["user"] = $this->input->post("txtUser");
        $data["pass"] = hash("sha256", $this->input->post("txtPass"));
        $data["email"] = $this->input->post("txtEmail");
        $data["group"] = $this->input->post("optGroup");

        if ($this->form_validation->run() == TRUE) {
            if ($this->admin_model->createAcc($data)) {
                $this->session->set_flashdata("warning", "Thêm mới tài khoản thành công");
                redirect("dienhoa-administrator/account/view_profile");
            } else {
                $this->session->set_flashdata("warning", "Không thể thêm mới");
                redirect("dienhoa-administrator/account/form_creat_acc");
            }
        } else {
            $this->form_creat_acc();
        }
    }

    //check user
    public function check_user($user) {
        $result = $this->admin_model->checkAdminName($user);
        if ($result) {
            return TRUE;
        } else {
            $this->form_validation->set_message('check_user', 'Tên đăng nhập đã có người dùng');
            return FALSE;
        }
    }

    public function check_email($email) {
        $result = $this->admin_model->checkAdminEmail($email);
        if ($result) {
            return TRUE;
        } else {
            $this->form_validation->set_message('check_email', 'Email đã có người dùng');
            return FALSE;
        }
    }

    //form tao nhom tai khoan
    public function form_creat_group() {
        $this->permissions_view();
        $data = array();
        $data = $this->SetMenu("");

        //lay tất cả các file trong controller admin
        $data["permission"] = array();
        $files = glob("application/controllers/dienhoa-administrator/*.php");
        $file_igore = array("dienhoa-administrator/permission", "dienhoa-administrator/base_controller");
        if (count($files)) {
            foreach ($files as $file) {
                $permis = "dienhoa-administrator/" . basename($file, ".php");
                if (!in_array($permis, $file_igore)) {
                    $data["permission"][] = $permis;
                }
            }
        }

        $this->render($this->load->view("dienhoa-administrator/account/creat_group_view", $data, TRUE));
    }

    //form sua group
    public function form_edit_group() {
        $this->permissions_view();
        $data = array();
        $data = $this->SetMenu("");

        $data["id"] = $this->uri->segment(4);

        $data["permission"] = array();
        $files = glob("application/controllers/dienhoa-administrator/*.php");
        $file_igore = array("dienhoa-administrator/permission", "dienhoa-administrator/base_controller");
        if (count($files)) {
            foreach ($files as $file) {
                $permis = "dienhoa-administrator/" . basename($file, ".php");
                if (!in_array($permis, $file_igore)) {
                    $data["permission"][] = $permis;
                }
            }
        }

        if ($data["id"]) {
            $rs = $this->admin_model->read($data["id"]);
            $data["group_id"] = $rs->admin_group_id;
            $data["group_name"] = $rs->admin_group_name;
            $data["permission_view"] = unserialize($rs->permission_view);
            $data["permission_edit_delete"] = unserialize($rs->permission_edit_del);
        }

        $this->render($this->load->view("dienhoa-administrator/account/edit_group_view", $data, TRUE));
    }

    //sua group
    public function edit_group() {
        $this->permission_del_edit();
        $data = array();
        $data = $this->SetMenu("");

        $id = $this->uri->segment(4);

        $this->form_validation->set_rules("txtName", "Tên nhóm", "trim|required");
        $this->form_validation->set_rules("permis_view[]", "Quyền xem", "required");
        $this->form_validation->set_rules("permis_edit_delete[]", "Quyền xóa sửa", "required");

        $data["name"] = $this->input->post("txtName", TRUE);
        $data["permis_view"] = serialize($this->input->post("permis_view"));
        $data["permis_edit_delete"] = serialize($this->input->post("permis_edit_delete"));

        if ($this->form_validation->run() == TRUE) {
            if ($this->admin_model->update($id, $data)) {
                $this->session->set_flashdata("warning", "Sửa thành công");
                redirect("dienhoa-administrator/account/view_profile");
            } else {
                $this->session->set_flashdata("warning", "Có lỗi trong quá trình sửa");
                redirect("dienhoa-administrator/account/form_edit_group");
            }
        } else {
            $this->form_creat_group();
        }
    }

    public function creat_group() {
        $this->permission_del_edit();
        $data = array();

        $this->form_validation->set_rules("txtName", "Tên nhóm", "trim|required");
        $this->form_validation->set_rules("permis_view[]", "Quyền xem", "required");
        $this->form_validation->set_rules("permis_edit_delete[]", "Quyền xóa sửa", "required");

        //lay id
        $id = $this->input->post("id");
        $data["name"] = $this->input->post("txtName", TRUE);
        $data["permis_view"] = serialize($this->input->post("permis_view"));
        $data["permis_edit_delete"] = serialize($this->input->post("permis_edit_delete"));

        if ($this->form_validation->run() == TRUE) {
            if ($this->admin_model->create($data)) {
                $this->session->set_flashdata("warning", "Thêm mới thành công");
                redirect("dienhoa-administrator/account/view_profile");
            } else {
                $this->session->set_flashdata("warning", "Có lỗi trong quá tạo quyền");
                redirect("dienhoa-administrator/account/form_creat_group");
            }
        } else {
            $this->form_creat_group();
        }
    }

    //xoa nhom tai khoan
    public function deleteGroup() {
        $this->permission_del_edit();
        $id = $this->uri->segment(4);
        if ($this->admin_model->deleteG($id)) {
            $this->session->flashdata("warning", "Xóa thành công");
            redirect("dienhoa-administrator/account/view_profile");
        } else {
            $this->session->flashdata("warning", "Có lỗi xảy ra");
            redirect("dienhoa-administrator/account/view_profile");
        }
    }

    //fonr sua thong tin
    public function form_edit_acc() {
        $this->permissions_view();
        $data = array();
        $data = $this->SetMenu("");

        $data["username"] = $this->session->userdata("user_name");
        $data["user"] = $this->session->userdata("user");
        $data["id"] = $this->session->userdata("ID");
        $data["groupID"] = $this->session->userdata("group");
        $data["group_admin"] = $this->admin_model->getGroup()->result();
        $this->render($this->load->view("dienhoa-administrator/account/edit_account_view", $data, TRUE));
    }

    //sua thong tin account
    public function editAcc() {
        $this->permission_del_edit();
        $id = $this->uri->segment(4);
        $data["username"] = $this->session->userdata("user_name");
        $this->form_validation->set_rules("txtName", "tên người dùng", "trim|required|xss_clean");
        $this->form_validation->set_rules("txtPass", "Mật khẩu", "trim|required|xss_clean|matches[txtRePass]");
        $this->form_validation->set_rules("txtRePass", "Nhập lại mật khẩu", "trim|required|xss_clean");
        $this->form_validation->set_rules("txtEmail", "Email", "trim|required|valid_email|callback_check_email");

        $data["name"] = $this->input->post("txtName");
        $data["pass"] = hash("sha256", $this->input->post("txtPass"));
        $data["email"] = $this->input->post("txtEmail");
        if ($id) {
            if ($this->form_validation->run() == TRUE) {
                if ($this->admin_model->editAcc($id, $data)) {
                    $this->session->set_flashdata("warning", "Sửa tài khoản thành công");
                    redirect("dienhoa-administrator/account/view_profile");
                } else {
                    $this->session->set_flashdata("warning", "Không thể sửa");
                    redirect("dienhoa-administrator/account/editAcc");
                }
            } else {
                $this->form_edit_acc();
            }
        }
    }
    
    public function deleteAcc(){
        $this->permission_del_edit();
        $id = $this->uri->segment(4);
        
        if($this->admin_model->deleteAcc($id)){
            $this->session->set_flashdata("warning","Xóa tài khoản thành công");
            redirect("dienhoa-administrator/account/view_profile");
        }else{
            $this->session->set_flashdata("warning","Không xóa được");
            redirect("dienhoa-administrator/account/view_profile");
        }
    }

    public function edit_group_for_acc(){
        $id = $this->uri->segment(4);
        $gID = $this->uri->segment(5);
        if($this->admin_model->editGAcc($id, $gID)){
            $this->session->set_flashdata("warning","Sửa nhóm tài khoản cho user thành công");
            redirect("dienhoa-administrator/account/view_profile");
        }else{
            $this->session->set_flashdata("warning","Có lỗi xảy ra");
            redirect("dienhoa-administrator/account/view_profile");
        }
        
    }
}

?>
